package com.hh.poidemo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class POIDemo2 {
	private Workbook wb;

	@Before
	@Ignore
	public void setUp() throws Exception {
		wb = WorkbookFactory.create(new File("e:/0.xlsx"));
	}

	@Test
	public void test() throws Exception {
//		int sheetNum = wb.getNumberOfSheets();
//		for (int i = 0; i < sheetNum; i++) {
//			Sheet sheet = wb.getSheetAt(i);
//			int lastRowNum = sheet.getLastRowNum();
//			System.out.println("firstRow:"+sheet.getFirstRowNum());
//			System.out.println("lastRow:"+sheet.getLastRowNum());
//			System.out.println(sheet.getSheetName());
//			for (int rowNum = 0; rowNum <= lastRowNum; rowNum++) {
//				Row row = sheet.getRow(rowNum);
//				if (row != null) {
//					int lastCellNum = row.getLastCellNum();
//					System.out.println("firstCellNum:"+row.getFirstCellNum());
//					System.out.println("lastCellNum:"+lastCellNum);
//					for (int cellNum = 0; cellNum < lastCellNum; cellNum++) {
//						Cell cell = row.getCell(cellNum);
//						if(cell != null){
//							System.out.print(cell.getStringCellValue() + "("+rowNum+","+cellNum+")\t");
//						}else{
//							System.out.println("("+rowNum+","+cellNum+")\t");
//						}
//					}
//					System.out.println();
//				}
//			}
//			System.out.println();
//			System.out.println();
//			System.out.println();
//		}
		copy("e:/2.xls","e:/1.xls");
	}
	public void copy(String from,String to) throws Exception{
		Workbook wb1 = WorkbookFactory.create(new File(from));
		File file = new File(to);
		file.createNewFile();
//		Workbook wb2 = WorkbookFactory.create(file);
		HSSFWorkbook wb2 = new HSSFWorkbook();
		for(int i=0;i<wb1.getNumberOfSheets();i++){
			Sheet sheet1 = wb1.getSheetAt(i);
//			Sheet sheet2 = wb2.createSheet(sheet1.getSheetName());
			HSSFSheet sheet2 = wb2.createSheet(sheet1.getSheetName());
			int rowIdx = sheet1.getLastRowNum();
			for(int j=0;j<=rowIdx;j++){
				Row row1 = sheet1.getRow(j);
//				Row row2 = sheet2.createRow(rowIdx);
				HSSFRow row2 = sheet2.createRow(rowIdx);
				for(int k = 0;i < row1.getLastCellNum();k++){
					Cell cell1 = row1.getCell(k);
					HSSFCell cell2 = row2.createCell(k);
					if(cell1 != null){
					System.out.println(cell1.getStringCellValue());
					cell2.setCellValue(cell1.getStringCellValue());
					}
				}
			}
		}
		OutputStream os = new FileOutputStream(file);
		wb2.write(os);
		wb2.close();
		os.close();
	}
}
